
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Transparency;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.net.URL;

import javax.imageio.ImageIO;

public class PosterUtil {

    static void drawPoster(Poster poster) throws Exception {
        long startTime = System.currentTimeMillis();
        String qrCodeUrl = poster.getQrCodeUrl();
        String avatarUrl = poster.getAvatarUrl();
        BufferedImage qrCodeImage = ImageIO.read(new URL(qrCodeUrl));
        BufferedImage newAvatar = ImageIO.read(new URL(avatarUrl));//circle(avatarUrl,poster.getAvatarWidth());;
        int width = poster.getWidth();
        int height = poster.getHeight();
        //画布
        BufferedImage canvas = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
        Graphics2D g = (Graphics2D) canvas.getGraphics();
        g.setBackground(Color.WHITE);//设置背景色
        g.clearRect(0, 0, width, height);

        // 设置文字抗锯齿
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        //画头像
        g.drawImage(newAvatar.getScaledInstance(poster.getAvatarWidth(), poster.getAvatarHeight(), Image.SCALE_SMOOTH), poster.getAvatarX(), poster.getAvatarY(), null);
        long startTime1 = System.currentTimeMillis();//剔除图片加载时间
        // 4. 写字（昵称）
        g.setColor(poster.getNameColor());
        g.setFont(poster.getNameFont());
        g.drawString(poster.getName(), poster.getNameX(), poster.getNameY());
        //画二维码
        g.drawImage(qrCodeImage.getScaledInstance(poster.getQrCodeWidth(), poster.getQrCodeHeight(), Image.SCALE_SMOOTH),
                poster.getQrCodeX(), poster.getQrCodeY(), null);

        //描述
        g.setColor(poster.getPositionColor());
        g.setFont(poster.getPositionFont());
        g.drawString(poster.getPosition(), poster.getPositionX(), poster.getPositionY());
        //公司
        g.setColor(poster.getCompanyColor());
        g.setFont(poster.getCompanyFont());
        g.drawString(poster.getCompany(), poster.getCompanyX(), poster.getCompanyY());
        //电话
        g.setColor(poster.getTelColor());
        g.setFont(poster.getTelFont());
        g.drawString(poster.getTel(), poster.getTelX(), poster.getTelY());

        //项目
        g.setColor(poster.getProjectColor());
        g.setFont(poster.getProjectFont());
        g.drawString(poster.getProject(), poster.getProjectX(), poster.getProjectY());

        //底部
        g.setColor(poster.getBottomColor());
        g.setFont(poster.getBottomFont());
        g.drawString(poster.getBottomInfo(), poster.getBottomX(), poster.getBottomY());

        g.dispose();

        File resultImg = new File("/data/chen/tmpFiles/demo_" + startTime + ".png");
        ImageIO.write(canvas, "png", resultImg);

        System.out.println("生成成功！");
        System.out.println("耗时: " + (System.currentTimeMillis()-startTime)/1000.0 + "s");
        System.out.println("不含图片加载耗时: " + (System.currentTimeMillis()-startTime1)/1000.0 + "s");
        System.out.println("生成文件路径: " + resultImg.getAbsolutePath());
    }


    private static BufferedImage circle(String avatar_img,int width) throws Exception {
        BufferedImage avatar = ImageIO.read(new URL(avatar_img));
        BufferedImage newAvatar = new BufferedImage(width, width, BufferedImage.TYPE_INT_ARGB);
        Ellipse2D.Double shape = new Ellipse2D.Double(0, 0, width, width);
        Graphics2D g2 = newAvatar.createGraphics();
        newAvatar = g2.getDeviceConfiguration().createCompatibleImage(width, width, Transparency.TRANSLUCENT);
        g2 = newAvatar.createGraphics();
        g2.setComposite(AlphaComposite.Clear);
        g2.fill(new Rectangle(newAvatar.getWidth(), newAvatar.getHeight()));
        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC, 1.0f));
        g2.setClip(shape);
        // 使用 setRenderingHint 设置抗锯齿
        g2 = newAvatar.createGraphics();
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.fillRoundRect(0, 0,width, width, width, width);
        g2.setComposite(AlphaComposite.SrcIn);
        g2.drawImage(avatar, 0, 0, width, width, null);
        g2.dispose();
        return newAvatar;
    }
}
